/**
 * 
 */
package org.jugjobs.botrepellent;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.http.HttpSession;

/**
 * @author lhaslup
 *
 */
public class BotRepellent {
	
	final static String CHALLENGE_TOTAL = "C_TOTAL";
	final static String CHALLENGE_DIGITS = "C_DIGITS";
	final static String CHALLENGE_DIGIT_IMAGES = "C_D_IMAGFES";
	
	final static String IMAGE_CHALLENGE_IMAGE = "I_C_IMAGE";
	final static String IMAGE_CLICK_TARGET = "I_C_TARGET";
	final static String IMAGE_CLICK_X = "I_C_X";
	final static String IMAGE_CLICK_X_SLOP = "I_C_X_SLOP";
	final static String IMAGE_CLICK_Y = "I_C_Y";
	final static String IMAGE_CLICK_Y_SLOP = "I_C_Y_SLOP";
	
	private final static String CONTROL = "CONTROL";
	private final static String C_SET = "SET";
	private final static String C_NOT_SET = "NOT_SET";

	public static Properties properties;
		
	public Properties getProperties() {
		if(properties==null) {
			properties=new Properties();
			try {
				InputStream is = getClass().getResourceAsStream("config.properties");
				properties.load(is);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return properties;
	}
	
	public void resetSession(HttpSession session) {
		session.setAttribute(CONTROL, C_NOT_SET);
	}

	private void setSession(HttpSession session) {
		if(!C_SET.equals((String)session.getAttribute(CONTROL))) {
			session.setAttribute(CONTROL,C_SET);
			int digitsInChallenge = Integer.parseInt(getProperties().getProperty("digits.in.challenge"));
			if(digitsInChallenge > 0) {
				int[] challengeDigits = new int[digitsInChallenge];
				String[] challengeDigitImages = new String[digitsInChallenge];
				int challengeDigitTotal = 0;
				for(int i=0; i<digitsInChallenge; i++) {
					challengeDigits[i] = (int)(Math.random()*10);
					challengeDigitImages[i] = (String)getProperties().get("digit.image."+challengeDigits[i]);
					challengeDigitTotal = challengeDigitTotal * 10 + challengeDigits[i];
				}
				session.setAttribute(CHALLENGE_DIGIT_IMAGES, challengeDigitImages);
				session.setAttribute(CHALLENGE_TOTAL, new Integer(challengeDigitTotal));
				session.setAttribute(CHALLENGE_DIGITS,challengeDigits);
			}
			int imageChallengeImageCount = 
				Integer.parseInt(getProperties().getProperty("images.avail.for.image.challenge"));
			if(imageChallengeImageCount>0) {
				int selectedImageNumber = (int)(Math.random()*imageChallengeImageCount);
				String selectedImageName = getProperties().getProperty("image."+selectedImageNumber);
				session.setAttribute(IMAGE_CHALLENGE_IMAGE, selectedImageName);
				String target =
					getProperties().getProperty("image.click.target."+selectedImageNumber);
				session.setAttribute(IMAGE_CLICK_TARGET, target);
				int xTarget = 
					Integer.parseInt(getProperties().getProperty("image.clickx."+selectedImageNumber));
				session.setAttribute(IMAGE_CLICK_X, new Integer(xTarget));
				int xTargetSlop = 
					Integer.parseInt(getProperties().getProperty("image.clickx.tolerance."+selectedImageNumber));
				session.setAttribute(IMAGE_CLICK_X_SLOP, new Integer(xTargetSlop));
				int yTarget = 
					Integer.parseInt(getProperties().getProperty("image.clicky."+selectedImageNumber));
				session.setAttribute(IMAGE_CLICK_Y, new Integer(yTarget));
				int yTargetSlop = 
					Integer.parseInt(getProperties().getProperty("image.clicky.tolerance."+selectedImageNumber));
				session.setAttribute(IMAGE_CLICK_Y_SLOP, new Integer(yTargetSlop));
			}
		}
	}
	
	String getDigitImage(HttpSession s, int n) {
		setSession(s);
		String[]names = (String[])s.getAttribute(CHALLENGE_DIGIT_IMAGES);
		return names[n];
	}
	
	String getImageChallengeImage(HttpSession s) {
		return (String)s.getAttribute(IMAGE_CHALLENGE_IMAGE);
	}
	
	public boolean validateDigitImageChallenge(HttpSession s, String response) {
		boolean result;
		try {
			Integer total = new Integer(response);
			result = total.equals((Integer)s.getAttribute(CHALLENGE_TOTAL));
		} catch (NumberFormatException e) {
			result = false;
		}
		return result;
	}
	
	public void resetChallenge(HttpSession s) {
		s.setAttribute(CONTROL,C_NOT_SET);

	}
}
